﻿using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ExcelData
{
    public class ExcelHelper
    {
        private static string GetCellValue(ICell cell)
        {
            if (cell != null)
            {
                return cell.StringCellValue;
            }
            else
            {
                return "";
            }
        }

        public static bool TestResult(string testReportPath)
        {
            try
            {
                DataTable dt = ImportExcel.ExcelToDataTable("Sheet1", testReportPath);
                StringBuilder stringBuilder1 = new StringBuilder();

                var 类型 = "";

                stringBuilder1.AppendLine(string.Format("{0},{1},{2},{3},{4},{5},{6}", "Work", "Channel", "Sensor", "NTC", "Connect", "K", "B"));

                foreach (DataRow dr in dt.Rows)
                {
                    try
                    {
                        string Work = dr.Table.Columns.Contains("工位") ? dr["工位"].ToString().Trim() : "";
                        string Channel = dr.Table.Columns.Contains("通道") ? dr["通道"].ToString().Trim() : "";
                        string Sensor = dr.Table.Columns.Contains("温度传感器类型") ? dr["温度传感器类型"].ToString().Trim() : "";
                        string Connect = dr.Table.Columns.Contains("接线方式") ? dr["接线方式"].ToString().Trim() : "";
                        string K = dr.Table.Columns.Contains("线性公式K") ? dr["线性公式K"].ToString().Trim() : "";
                        string B = dr.Table.Columns.Contains("线性公式B") ? dr["线性公式B"].ToString().Trim() : "";
                        string NTC = "";
                        if (!string.IsNullOrEmpty(Sensor))
                        {
                            类型 = Sensor;
                        }
                        else
                        {
                            Sensor = 类型;
                        }

                        var lx = Sensor.Split('\\');

                        if (lx.Length == 1)
                        {
                            stringBuilder1.AppendLine(string.Format("{0},{1},{2},{3},{4},{5},{6}", Work, Channel, Sensor, NTC, Connect, K, B));
                        }
                        else if (lx.Length >= 2)
                        {
                            for (int a = 0; a < lx.Length; a++)
                            {
                                var lx1 = lx[a];
                                //
                                if (lx1.ToLower().Contains("k"))
                                {
                                    Sensor = "NTC";
                                    //NTC = "";
                                    var lx11 = lx1.Split('-');
                                    if (lx11.Length == 1)
                                    {
                                        NTC = lx11[0];
                                    }
                                    else if (lx11.Length > 1)
                                    {
                                        var lx22 = lx11[1];
                                        NTC = lx11[1];
                                    }
                                }
                                else
                                {
                                    Sensor = lx1;
                                }

                                //  var Work = ""; var Channel = ""; var Sensor = ""; var NTC = ""; var Connect = ""; var K = ""; var B = "";
                                stringBuilder1.AppendLine(string.Format("{0},{1},{2},{3},{4},{5},{6}", Work, Channel, Sensor, NTC, Connect, K, B));


                            }
                        }
                    }
                    catch (Exception ex)
                    {
                    }
                }

                SaveExcel(stringBuilder1);

                return true;
            }
            catch (Exception ex)
            {

                return false;
            }
        }

        private static void SaveExcel(StringBuilder stringBuilder)
        {
            var csvFile = @"D:\墨氪智能\ROBOT\ROBOT-ThermalX\ThermalX-中车研究院\Save.csv";
            FileStream fileStream = new FileStream(csvFile, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Read);
            byte[] bytes = Encoding.UTF8.GetBytes(stringBuilder.ToString());
            fileStream.Write(bytes, 0, bytes.Length);
            fileStream.Flush(true);
        }
    }
}
